<template>
  <div class="basic-info">
    <div class="basic-body common-content">
      <el-scrollbar style="height: 100%;">
        <div class="scrollbar-box">
          <div class="comp-item-group">
            <div class="comp-item-box">
              <div class="comp-item-title">填写应用信息</div>
              <div class="comp-item-cont">
                <el-form
                    :model="basicForm"
                    :rules="basicRules"
                    ref="basicForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用名称" prop="appName">
                        <el-input v-model="basicForm.appName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用版本号" prop="appVersion">
                        <el-input v-model="basicForm.appVersion"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用类型" prop="appType">
                        <el-select
                            class="w100"
                            v-model="basicForm.appType"
                            placeholder="选择应用类型">
                          <el-option label="独立应用" value="1"></el-option>
                          <el-option label="组合应用" value="2"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="数据入库" prop="appIndb">
                        <el-select
                            class="w100"
                            v-model="basicForm.appIndb"
                            placeholder="数据是否入库">
                          <el-option label="入库" value="0"></el-option>
                          <el-option label="不入库" value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="basicForm.appIndb==='0'">
                      <el-form-item label="数据库名称" prop="appVersion">
                        <el-input v-model="basicForm.appDBName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="所属资源" prop="resourceId">
                        <el-select
                            class="w100"
                            v-model="basicForm.resourceId"
                            placeholder="选择所属资源">
                          <el-option
                              v-for="(item, index) in resource"
                              :key="index"
                              :label="item.resName"
                              :value="item.vendorId"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否监听" prop="isMonitor">
                        <el-select
                            class="w100"
                            v-model="basicForm.isMonitor"
                            placeholder="数据是否监听">
                          <el-option label="监听" value="0"></el-option>
                          <el-option label="不监听" value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="是否切换" prop="isSwitch">
                        <el-select
                            class="w100"
                            v-model="basicForm.isSwitch"
                            placeholder="数据是否切换">
                          <el-option label="切换" value="0"></el-option>
                          <el-option label="不切换" value="1"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="版本说明" prop="appRemarks">
                        <el-input
                            type="input"
                            v-model="basicForm.appRemarks"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="场景描述" prop="appScene">
                        <el-input
                            type="textarea"
                            v-model="basicForm.appScene"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>

                <div class="comp-item-btn-group">
                  <el-button type="warning">重置数据</el-button>
                  <el-button type="primary" @click="appAdd">录入信息</el-button>
                </div>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">填写执行计划</div>
              <div class="comp-item-cont">
                <el-form
                    :model="planForm"
                    :rules="planRules"
                    ref="planForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="计划名称" prop="planName">
                        <el-input v-model="planForm.planName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="计划类型" prop="executeType">
                        <el-select
                            class="w100"
                            v-model="planForm.executeType"
                            placeholder="请选择计划类型">
                          <el-option label="即时执行" value="0"></el-option>
                          <el-option label="常驻进程" value="1"></el-option>
                          <el-option label="周期执行" value="2"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="执行单元" prop="executeUnit">
                        <el-select
                            class="w100"
                            v-model="planForm.executeUnit"
                            placeholder="请选择执行单元">
                          <el-option label="每天" value="0"></el-option>
                          <el-option label="每周" value="1"></el-option>
                          <el-option label="每月" value="2"></el-option>
                          <el-option label="每年" value="3"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="时间" prop="executeTime">
                        <el-input v-model="planForm.executeTime"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8" v-if="this.planForm.executeType == 2">
                      <el-form-item label="最小周期" prop="executeDay">
                        <el-input v-model="planForm.executeDay"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input v-model="planForm.remarks"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">录入运行环境</div>
              <div class="comp-item-cont">
                <el-form
                    :model="environmentForm"
                    :rules="environmentRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="使用系统" prop="systemId">
                        <el-select
                            class="w100"
                            v-model="environmentForm.systemId"
                            placeholder="请选择使用系统">
                          <el-option
                              v-for="item in systemOptions"
                              :key="item.id"
                              :label="item.systemName"
                              :value="item.id">
                          </el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小CPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireCPU"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小存储单元">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireDisk"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="最小GPU">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireGPU"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="运行内存要求">
                        <el-input
                            type="number"
                            v-model="environmentForm.requireMemory"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item class="up" label="上传环境">
                        <el-upload
                            class="upload-demo"
                            :action="url + 'api/editPARequireFile'"
                            :on-success="success"
                            :headers="token">
                          <el-button type="primary">点击上传</el-button>
                        </el-upload>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="备注" prop="remarks">
                        <el-input
                            type="input"
                            v-model="environmentForm.remarks"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">配置应用监控</div>
              <div class="comp-item-cont">
                <el-form
                    :model="monitorForm"
                    :rules="monitorRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="监控频率/s">
                        <el-input v-model="monitorForm.monitorRate"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="监控类型" prop="monitorType">
                        <el-select
                            class="w100"
                            v-model="monitorForm.monitorType"
                            placeholder="请选择监控类型">
                          <el-option label="进程监控" value="0"></el-option>
                          <el-option label="数据监控" value="1"></el-option>
                          <el-option label="全部" value="2"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </div>

            <div class="comp-item-box">
              <div class="comp-item-title">应用故障切换</div>
              <div class="comp-item-cont">
                <el-form
                    :model="switchForm"
                    :rules="switchRules"
                    ref="environmentForm"
                    size="small"
                    label-width="100px">
                  <el-row :gutter="15">
                    <el-col :span="8">
                      <el-form-item label="应用切换名称">
                        <el-input v-model="switchForm.switchName"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="应用切换地址">
                        <el-input v-model="switchForm.switchAddress"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>

              <div class="comp-item-btn-group">
                <el-button v-if="!FappId" type="primary" size="small" @click="btnAll">保存</el-button>
                <el-button v-else type="primary" size="small" @click="ediBtn">保存修改</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
  </div>
</template>

<script>
import {
  get_getResource,
  put_AApplication,
  put_insertAPlan,
  put_aApplicationInfoAdd,
  get_getAppliesInfo,
  put_editAApplication
} from "@/apis/appLC/applc";
import {
  put_monitorAdd,
  put_switchAdd,
  get_getSystem,
} from "@/apis/assembly/assembly";
import store from "@/store";

export default {
  data() {
    return {
      activeNames: ["1"],
      activeNames2: ["1", "2", "3", "4"],
      resourceActiveNames: ["1"],
      basicForm: {
        //基本信息表单
        appName: "",
        appVersion: "",
        appIndb: "0",
        appDBName: "",
        isMonitor: "0",
        isSwitch: "1",
        appRemarks: "",
        remarks: "",
        resourceId: "",
        appScene: "",
        appType: "",
        id: "",
      },
      basicRules: {
        //基本信息验证
      },
      planForm: {
        planName: "",
        executeType: "0",
        executeUnit: "",
        executeTime: "",
        executeDay: "",
        remarks: "",
        id: "",
      },
      planRules: {},
      monitorForm: {
        //配置监控表单
        monitorRate: "",
        monitorType: "",
        pId: "",
        id: "",
      },
      monitorRules: {}, //监控表单校验
      environmentForm: {
        pId: "",
        systemId: "",
        requireCPU: "",
        requireDisk: "",
        requireGPU: "",
        requireMemory: "",
        remarks: "",
        id: "",
        requirePath: "",
        file: "",
        //录入环境表单
      },
      environmentRules: {},
      //--------------------------------------
      switchForm: {
        //组件切换表单
        switchAddress: "",
        switchName: "",
        pId: "",
        id: "",
      },
      switchRules: {},
      resource: [], //资源外键
      appId: "",
      isAddApp: false,
      systemOptions: [],
      FappId: "",
      Finfo: "",
      url: window.MyUrl.BaseUrl,
      token: "",
    };
  },
  created() {
    this.token = {
      token: sessionStorage.getItem("token"),
    };
    this.getResource();
    this.getSystem();
    this.FappId = this.$route.query.appId;
    if (this.FappId) {
      get_getAppliesInfo({appId: this.FappId}).then((res) => {
        let {
          applies, arelation, aplan, pamonitor, parequire, paswitch, adata,
        } = res.data.data;
        let {data} = res.data;
        if (paswitch) {
          for (const key in this.switchForm) {
            this.switchForm[key] = paswitch[key] + "";
          }
          this.switchForm.pId = paswitch.paId;
        }
        if (applies) {
          for (const key in this.basicForm) {
            this.basicForm[key] = applies[key] + "";
          }
        }

        if (aplan) {
          for (const key in this.planForm) {
            this.planForm[key] = aplan[key] + "";
          }
        }
        if (pamonitor) {
          for (const key in this.monitorForm) {
            this.monitorForm[key] = pamonitor[key] + "";
          }
          this.monitorForm.pId = pamonitor.paId;
        }

        if (parequire) {
          for (const key in this.environmentForm) {
            this.environmentForm[key] = parequire[key] + "";
          }
          this.environmentForm.pId = parequire.paId;
          this.environmentForm.file = parequire.requirePath;
        }

        this.Finfo = data;
      });
    }
  },
  mounted() {
  },
  methods: {
    //录入基本信息组件
    getFile(val) {
      let data = new FormData();
      data.append("file", val.file);
    },
    //获取上传文件信息
    success(res, file) {
      this.environmentForm.requirePath = res.data;
    },
    //保存修改
    ediBtn() {
      let opt = {
        aPlan: this.planForm,
        pRequire: this.environmentForm,
        pSwitch: this.basicForm.isSwitch == 0 ? this.switchForm : "",
        pMonitor: this.basicForm.isMonitor == 0 ? this.monitorForm : "",
        Applies: this.basicForm,
      };
      put_editAApplication({...opt}).then((res) => {
        let {msg, code} = res.data;
        if (code == 0) {
          this.$message.success(msg);
          this.$router.push({
            name: "fldata",
            query: {add: "add", appid: this.FappId},
          });
        }
      });
    },
    //系统下拉
    getSystem() {
      get_getSystem().then((res) => {
        this.systemOptions = res.data.data;
      });
    },
    //保存后面三张数据
    btnAll() {
      this.planForm.appId = this.appId;
      this.environmentForm.paId = this.appId;
      this.switchForm.paId = this.appId;
      this.monitorForm.paId = this.appId;
      let opt = {
        aPlan: this.planForm,
        pRequire: this.environmentForm,
        pSwitch: this.basicForm.isSwitch == 0 ? this.switchForm : "",
        pMonitor: this.basicForm.isMonitor == 0 ? this.monitorForm : "",
      };

      put_aApplicationInfoAdd({...opt}).then((res) => {
        let {code, msg} = res.data;
        if (code == 0) {
          this.$message.success(msg);
          this.$router.push({
            name: "fldata",
            params: {add: "add"},
          });
        }
      });
    },
    next() {
    },
    handleChange(val) {
    },
    //获取资源外键
    getResource() {
      get_getResource().then((res) => {
        this.resource = res.data.data;
      });
    },
    // 录入应用
    appAdd() {
      put_AApplication(this.basicForm).then((res) => {
        let {data, code, msg} = res.data;
        if (code === 0) {
          this.$message.success(msg);
          this.appId = data;
          sessionStorage.setItem("isApp", this.appId);
          store.commit("fappId", this.appId); // 保存组件id
          this.$confirm("是否录入应用附属信息?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.isAddApp = true;
          }).catch(() => {
            this.$router.push({
              name: "fldata",
              params: {add: "add"},
            });
          });
        }
      });
    },
    //录入执行计划
    planAdd() {
      this.planForm.AId = this.appId;
      put_insertAPlan(this.planForm).then((res) => {
        let {data, code, msg} = res.data;
        if (code == 0) {
          this.$message.success(msg);
        }
      });
    },
    //配置监控
    submitmonitorForm() {
      this.monitorForm.pId = this.appId;
      put_monitorAdd(this.monitorForm).then((res) => {
        let {code, msg} = res.data;
        if (code == 0) {
          this.$message.success(msg);
        }
      });
    },
    //切换配置
    submitswitchForm() {
      this.switchForm.pId = this.appId;
      put_switchAdd(this.switchForm).then((res) => {
        let {code, msg} = res.data;
        if (code == 0) {
          this.$message.success(msg);
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>

  .scrollbar-box {
    padding: 15px;
  }

  .comp-item-group {
    .comp-item-box {
      margin-bottom: 15px;

      .comp-item-title {
        height: 40px;
        color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: rgba(3, 68, 108, 0.8);
        font-size: 17px;
        font-weight: bold;
      }

      .comp-item-cont {
        padding: 15px;
        border: solid 1px rgba(3, 68, 108, 0.5);
      }
    }

    .comp-item-btn-group {
      text-align: center;
      margin-top: 15px;
    }

    /deep/ .el-input-group__append {
      background-color: #66b1ff;
      border-color: #66b1ff;
      color: #ffffff;
    }
  }

  .btnBot {
    width: 100%;
    text-align: center;
    margin-top: 10px;
  }

  .basic-info {
    height: 100%;
    width: 100%;

    .basic-body {
      height: 100%;
      width: 100%;
      overflow: hidden;
    }

    .cont-bottom {
      background: white;
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
    }

    /deep/ .el-collapse-item__header {
      position: relative;
    }

    .cont-main {
      height: 100%;
      padding: 15px;

      .collapseTitle {
        margin-left: 10px;
      }

      .interfaceItems {
        margin-top: 30px;
      }

      .cont_body {
        margin-top: 15px;

        .timeLine {
          width: 1000px;
          margin: 0 auto;

          /deep/ .el-step__title {
            margin-left: -10px;
          }
        }
      }
    }

    .basic {
      display: flex;
      align-items: center;

      .dfc {
        margin-right: 30px;
      }
    }

    .files {
      padding: 0 15px;
      display: flex;
    }

    .addbtn {
      margin-left: 10px;
    }

    .waydiv {
      padding: 0 15px;
    }

    .waylist {
      margin-right: 20px;
    }

    .systm {
      padding: 0 15px;
    }

    .min {
      margin-right: 30px;
    }

    .wid {
      width: 500px;
    }

    .viewbox {
      width: 100%;
      border: 1px solid #dcdcdc;
      position: relative;
      padding: 30px 10px 10px 10px;
      // margin-bottom: 15px;
      .sptitle {
        position: absolute;
        top: -12px;
        background: white;
        left: 10px;
      }

      .jkming {
        margin-right: 50px;
        padding-left: 12px;
        margin-bottom: 15px;
      }
    }
  }

  .btn-class {
    background: #267ff6;
    border: none;
    color: white;
    border-radius: 15px;
    line-height: 30px;
    height: 30px;
    width: 100px;
    position: absolute;
    left: 50%;
    margin-left: -50px;
    cursor: pointer;
  }
</style>
